// pages/personinformation/personinformation.js
const app = getApp()
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
var uploadImage = require('../../utils/uploadFile.js');
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    choosenav:1,
    telphone:Number,
    personal_img:'', //形象照
    headimgurl:'',
    name:'',
    region:'',
    career:'',
    follow_me: '', //粉丝
    me_collect: '', //收藏
    me_follow: '',//关注的人
    sex:'',
    birthday:'',
    height:'',
    weight:'',
    school:'',
    is_follow:'', //是否关注了
    check:'',
    videoarr:[],
    id:'', //携带过来的id
    page: 1,
    limit: 5,
    nodata: true,
    nodate: true,
    nodatephoto:true, //相册无数据
    is_contact:Number,
    hasdemand_id:null,
    photoarr:[], //照片
    introduction:'', //介绍
    checkrole:'',//判断身份
    age:Number, //年龄
    chexkshare:false,
    shareimg:'', //分享的图片
    checkchat:false, //联系
    hasshowname:false,
    user_id:wx.getStorageSync("user_id"), //用户自己的id
    checkcontact:true,
    checkbtn:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //我的作品
  my_works: function (page, limit,more){
    var that = this;
    const data = {
      page: page,
      limit: limit,
      user_id: that.data.id
    }

    http.request({
      url: 'mini/v1/personal/my_works',
      data: data,
      success: function (res) {
        wx.stopPullDownRefresh()
        if (!!more) {
          that.setData({
            videoarr: that.data.videoarr.concat(res.data.data),
            nodate: true
          })

          if (res.data.data.length == 0) {
            that.setData({
              nodata: false
            });
          }
        } else {
          that.setData({
            videoarr: res.data.data,
            nodate: true
          })

          if (res.data.data.length == 0) {
            that.setData({
              nodate: false
            });
          }
        }
      }
    })
  },
  //我的照片
  my_photo:function(){
    var that = this;
    const data = {
      user_id: that.data.id
    }

    http.request({
      url: 'mini/v1/personal/my_photo',
      data: data,
      success: function (res) {
        console.log(res.data)
          that.setData({
            photoarr:res.data
          });

          if (res.data.length == 0) {
            that.setData({
              nodatephoto: false
            });
          }
        }
    })
  },
  onLoad: function (options) {
    this.setData({
      id:options.id,
      hasdemand_id: options.hasdemend,
      checkrole:wx.getStorageSync('checkrole')
    })

    if(options.id == wx.getStorageSync("user_id")){
        this.setData({
          checkcontact:false,
          checkbtn:true
        })
    }
    
    this.shareimg()
    if (options.name){
      this.setData({
        check:'yes'
      })
    }else{
      this.setData({
        check: 'no'
      })
    }
    
    this.my_photo()
    var that = this;
    var ids = '';
    var url='';
    if(!!options.id){
      ids = options.id
      url = 'mini/v1/login/user';
      this.my_works(1, 10)
    }else{
      ids = ''
      url = 'mini/v1/personal/info';
      this.my_works(1,10)
    }
    var data = {
      id: ids
    }
    http.request({
      url: url,
      data: data,
      success: function (res) {
        that.setData({
          personal_img: res.data.personal_img,
          headimgurl: res.data.headimgurl,
          name: res.data.name,
          region: res.data.region,
          career: res.data.career,
          introduction: res.data.introduction,
          school: res.data.school,
          weight: res.data.weight,
          height: res.data.height,
          birthday: res.data.birthday,
          sex: res.data.sex,
          follow_me: res.data.follow_me,
          me_collect: res.data.me_collect,
          telphone: res.data.phone,
          is_follow: res.data.is_follow,
          me_follow: res.data.me_follow,
          is_contact: res.data.is_contact,
          age:res.data.age
        })
      }
    })
  },
  onShow:function(){
    if(wx.getStorageSync('checklogin') == 'checklogin'){
        this.setData({
          hasshowname:true
        })
    }

    //if()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onReachBottom: function () {
    if (!!this.data.videoarr && this.data.videoarr.length !== 0){
      if (this.data.choosenav == 1) {
        this.setData({
          page: this.data.page + 1
        })
        this.my_works(this.data.page, this.data.limit, 'more');
      }
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return util.share()
  },
  //分享图片图片获取
   shareimg:function(){
     var that = this
    var data = {
      user_id: !!this.data.id ? this.data.id : ''
    }
    http.request({
      url: 'mini/v1/personal/business_card',
      data: data,
      success: function (res) {
        that.setData({
          shareimg:res.data.url
        })
      }
    })
   },
  //选择导航
   choosenav(e) {
      this.setData({
        choosenav: e.currentTarget.dataset.index
      })
    },
  //聊天
    chatwx:function(){
      this.setData({
        checkchat:false
      }); 
      wx.navigateTo({
        url: '../new_detail/new_detail?id='+this.data.id+'&&name='+this.data.name
      })
    },
  //拨打电话
    callPhone:function(){
      var that = this;
      wx.makePhoneCall({
        phoneNumber: `${this.data.telphone}`,
        success: function (res) {
          that.setData({
            checkchat:false
          })
          if (!!that.data.hasdemand_id){
              var data = {
                demand_id: that.data.hasdemand_id,
                user_id: that.data.id
              }
              
              http.request({
                url: 'mini/v1/release/demand_contact',
                data: data,
                methods: 'post',
                success: function (res) {
                  
                }
              })
          }
        }
      })
    },
  //关注取关 
    is_follow:function(){
      var is_follow = this.data.is_follow == 0 ? '1' : '0';
      this.setData({
        is_follow:is_follow
      })

      var data = {
        id: this.data.id
      }
      http.request({
        url: this.data.is_follow == 1 ? 'mini/v1/release/follow_on' : 'mini/v1/release/follow_off', 
        data: data,
        methods:'post',
        success: function (res) {
        }
      })
    },
  //上传作品
   upvideo:function(){
     var name = 'videoarr';
     var that = this;
     util.upvideo(name, that)
   },
   //上传图片
    upphoto:function(){
      var name = 'photoarr';
      var that = this;
      util.upphoto(name, that)
      setTimeout(function(){
        that.setData({
          nodatephoto: true
        });
      },1000)
    },
  //删除发布的作品
    works_delete: function (e) {
      var that = this;
      var id = e.currentTarget.dataset.id;
      var num = e.currentTarget.dataset.index;
      var type = e.currentTarget.dataset.type
      wx.showModal({
        title: '提示',
        content: '确定删除？ ',
        success(res) {
          if (res.confirm) {
            http.request({
              url: type == 'video' ? 'mini/v1/release/works' : 'mini/v1/release/photo',
              data: { id: id },
              methods: 'delete',
              success: function (res) {
                var lists = type == 'video' ? that.data.videoarr : that.data.photoarr
                lists.splice(num, 1);
                if(type == 'video'){
                  that.setData({
                    videoarr: lists
                  });
                }else{
                  that.setData({
                    photoarr: lists
                  });
                }
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    },
  //分享名片
    shareImg:function(){
      var that = this
      that.setData({
        chexkshare:true
      })
    },
  //生成名片
    lookcard:function(){
      var that = this
      wx.previewImage({
        urls: [that.data.shareimg],
        success:function(){
          that.setData({
            chexkshare:false
          })
        }
      })
    },
  //关闭分享弹层
    caccercheckshare:function(){
      this.setData({
        chexkshare:false,
        checkchat:false
      });
    },
  //立即联系
    chat:function(){
      this.setData({
        checkchat:true
      });
    },
    onShareAppMessage: function( options ){
      　　var that = this;
      　　// 设置菜单中的转发按钮触发转发事件时的转发内容
      　　var shareObj = {
      　　　　title: `我是演员“${that.data.name}”，这是我的个人名片请查收～`,
      　　　　path: '/other/personinformation/personinformation?id='+that.data.id,
             imageUrl:that.data.shareimg //显示图片长宽比是 5:4
      　　};
          this.setData({
            chexkshare:false
          })
      　　// 返回shareObj
      　　return shareObj;
    },
  //查看摩卡详情大图
     lookmuka:function(e){
      const url = e.currentTarget.dataset.img
      var that = this
      wx.previewImage({
        urls: [url],
        success:function(){
          
        }
      })
     }
})